package com.example.demo.controller;


import com.example.demo.common.BusinessException;
import com.example.demo.common.ErrorEnum;
import com.example.demo.config.myannotation.code.LogAnno.LogAnno;
import com.example.demo.entity.Token;
import com.example.demo.entity.User;
import com.example.demo.outstyle.Message;
import com.example.demo.outstyle.TableData;
import com.example.demo.outstyle.WebUser;
import com.example.demo.service.LoginService;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "user")
@Controller
@RequestMapping(value = "/logic/user")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userServiceImpl;
    @Autowired
    LoginService loginServiceImp;


    @ApiOperation(value = "插入用户信息")
    @PostMapping("/addUserInfo")
    @ResponseBody
    @LogAnno(operatType = "插入用户信息")
    public Message addUser(User user) {
        user.setId(0); //不set无法获得正确的自增后的数值
        User result = userServiceImpl.addUser(user);
        Message msg = new Message();
        if(result.getId() != null){
            msg.setMsg("成功创建用户");
            msg.setState(0);
        } else{
            throw new BusinessException(ErrorEnum.E_chongfu);
        }
        msg.setData(result);
        return msg;
    }

    @ApiOperation(value = "分页查询用户信息")
    @PostMapping("/select")
    @ResponseBody
    @LogAnno(operatType = "分页查询用户信息")
    public TableData<User> searchUser(int page,int limit,User example) {
        TableData<User> result = new TableData();

        List<User> userList = userServiceImpl.searchByExample(page, limit,example);
        int allCount = userServiceImpl.searchByExampleCount(example);

        result.setState(0);
        result.setCount(allCount);
        result.setData(userList);
        result.setMsg("成功加载");

        return result;
    }

    @ApiOperation(value = "删除用户信息")
    @PostMapping("/deleteUserInfo")
    @ResponseBody
    @LogAnno(operatType = "删除用户信息")
    public Message deleteUser(int id) {
        int result = userServiceImpl.deleteByPrimaryKey(id);
        Message msg = new Message();
        if(result > 0){
            msg.setMsg("成功删除");
            msg.setState(0);
        } else{
            throw new BusinessException(ErrorEnum.E_shanchushibai);
        }
        msg.setData(result);
        return msg;
    }

    @ApiOperation(value = "更改用户信息")
    @PostMapping("/updateUserInfo")
    @ResponseBody
    @LogAnno(operatType = "更改用户信息")
    public Message modifyUser(User user) {
        int result = userServiceImpl.updateByPrimaryKey(user);

        Message msg = new Message();
        if(result > 0){
            msg.setMsg("成功修改");
            msg.setState(0);
        } else{
            throw new BusinessException(ErrorEnum.E_xiugaishibai);
        }
        msg.setData(result);
        return msg;
    }

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    @ResponseBody
    @LogAnno(operatType = "登录")
    public Message login(String username, String password ) {
        User result = userServiceImpl.login(username, password);

        Message msg = new Message();
        if (result != null){
            WebUser user = new WebUser();
            user.setUid(result.getUid());
            user.setNickname(result.getNickname());
            user.setUsertype(result.getUsertype());
            user.setToken(loginServiceImp.getToken(new User(username)));
            msg.setState(0);
            msg.setMsg("登录成功");
            msg.setData(user);
        }else{
           throw new BusinessException(ErrorEnum.E_denglushibai);
        }
        return msg;
    }
    @ApiOperation(value = "登出")
    @PostMapping("/logout")
    @ResponseBody
    @LogAnno(operatType = "登出")
    public Message logout() {
        Message msg = new Message();
        int result = 0;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String token = request.getHeader("token");
        if (token == null)
            throw new BusinessException(ErrorEnum.E_wuyonghu);
        else
             result = loginServiceImp.logout(token);
        if (result !=0){
            msg.setState(0);
            msg.setMsg("登出成功");
        }else {
            throw new BusinessException(ErrorEnum.E_dengchushibai);
        }
        return msg;
    }
    @ApiOperation(value = "查询当前用户信息")
    @PostMapping("/selectcurrentuser")
    @ResponseBody
    @LogAnno(operatType = "查询当前用户信息")
    public Message selectCurrentUser() {
        Message msg = new Message();
        User user = new User(null);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String token = request.getHeader("token");
        if (token == null){
            throw new BusinessException(ErrorEnum.E_wuyonghu);
        }
        else{
            user = loginServiceImp.selectCurrentUser(token);
        }
        if (user!= null){
            msg.setState(0);
            msg.setMsg("查询成功");
            msg.setData(user);
        }else {
            throw new BusinessException(ErrorEnum.E_wuyonghu);
        }
        return msg;
    }
}
